package com.xmutca.cloud.auth.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

import com.xmutca.cloud.auth.security.SecurityAuthenticationProvider;
import com.xmutca.cloud.auth.security.SecurityUserDetailService;

/**
 * @author: 彭伟煌(pengweihuang@xmutca.com)
 * @create: 2017-01-10 09:50
 */
@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true, prePostEnabled = true)//开启权限注解
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService customerUserDetailService() {
        return new SecurityUserDetailService();
    }
    
    @Bean
    public AuthenticationProvider customerAuthenticationProvider() {
    	return new SecurityAuthenticationProvider();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            //该过滤哪些请求
            .authorizeRequests()
            	.anyRequest()
            	.authenticated()

            //配置登陆地址
            .and()
	            .formLogin()
	            .loginPage("/login")
	            .permitAll()

            //配置退出地址
            .and()
	            .logout()
	            .logoutUrl("/logout")
	            .permitAll();
/*
        http
            //记住我
            .rememberMe()
            .userDetailsService(customerUserDetailService())
            .tokenValiditySeconds(30 * 24 * 60 * 60)
            .key("xmutca").and()

            //防止二次登陆
            .sessionManagement()
            .maximumSessions(1);*/
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(customerAuthenticationProvider());
        auth.userDetailsService(customerUserDetailService());
    }
}